What is @opencensus/core?
The @opencensus/core package is a set of libraries for collecting, processing, and exporting telemetry data (metrics and traces) for analysis to improve the performance and reliability of applications. It is part of the OpenCensus project, which aims to provide a single, high-quality telemetry collection framework across multiple languages.
Tracing
This feature allows the collection and export of trace data, which helps in understanding the flow of requests through various services and in identifying bottlenecks and latency issues.
const { Tracing } = require('@opencensus/core');
const tracing = Tracing.instance;
// Configure tracing
tracing.start({samplingRate: 1});
// Create a custom span
const rootSpan = tracing.tracer.startRootSpan({name: 'main'}, rootSpan => {
// Do work within the span
rootSpan.end(); // End the span
});
Metrics
This feature enables the collection and aggregation of metrics data, such as counts or latencies, which can be used for monitoring application performance and health.
const { Metrics, MeasureUnit } = require('@opencensus/core');
const metrics = Metrics.instance;
// Create a measure
const requestCountMeasure = metrics.createMeasureInt64('request_count', MeasureUnit.UNIT, 'Count of requests received');
// Create and register a view to aggregate the data
metrics.createView('request_count_view', requestCountMeasure, 'count', [], 'The count of requests', []);
// Record data
metrics.record([{measure: requestCountMeasure, value: 1}]);